<?php
/**
 * 首页接口
 * @author czb
 *
 */
require(dirname(dirname(__FILE__))."/includes/initInterface.php");
require(dirname(dirname(__FILE__))."/includes/cls_newsModel.php");
$action  = isset($_REQUEST['act']) ? trim($_REQUEST['act']) : 'initTrends';
$login  = new News();
$login->$action();
Class News {
	private $m;//自己的model
	private $jsonp;
	public function __construct(){
	
		$this->jsonp = new JSONP();
		//基础信息构建
		$this->m = new newsModel();
		//微信分享参数构建
	}
	public function initTrends(){
		global $_CODE;
// 		$uid = isset($_REQUEST['uid'])?intval($_REQUEST['uid']):0;
		$wxid = $_SESSION['wxid']?$_SESSION['wxid']:0;
		$result = $this->m->get_trends_info($wxid);
		$result['wxid'] = $wxid;
		die($this->jsonp->JSONP($_CODE['NEWS_INITTRENDS_SUCCESS'],$result));
	}
	
	public function getMyCollect()
	{
		global $_CODE;
		$wxid = $_SESSION['wxid']?$_SESSION['wxid']:0;
		if($wxid>0)
		{
			$res = $this->m->get_my_collect($wxid);
			$result['res'] = $res;
			$result['wxid'] = $wxid;
			die($this->jsonp->JSONP($_CODE['NEWS_GETMYCOLLECT_SUCCESS'],$result));
		}else
		{
			die($this->jsonp->JSONP($_CODE['NEWS_GETMYCOLLECT_LOSE_UID']));
		}
	}
	
	public function collectNew()
	{
		global $_CODE;
		if(isset($_REQUEST['wxid'])&&isset($_REQUEST['news_id']))
		{
			$wxid = intval($_REQUEST['wxid']);
			$news_id = intval($_REQUEST['news_id']);
			$result = $this->m->collect_new($wxid,$news_id);
			if($result['id']){
				die($this->jsonp->JSONP($_CODE['NEWS_COLLECTNEW_SUCCESS'],$result));
			}else{
				die($this->jsonp->JSONP($_CODE['NEWS_COLLECTNEW_ERROR']));
			}
		}else
		{
			die($this->jsonp->JSONP($_CODE['NEWS_COLLECTNEW_LOSE_UID']));
		}
	}
	
	public function getNewsInfo()
	{
		global $_CODE;
		if(isset($_REQUEST['news_id']))
		{
			$news_id = intval($_REQUEST['news_id']);
			$wxid = $_SESSION['wxid']?$_SESSION['wxid']:0;
			$result = $this->m->get_news_info($news_id,$wxid);
			if(!$result){
				die($this->jsonp->JSONP($_CODE['NEWS_GETNEWSINFO_ERROR']));
			}else{
				$info = $this->m->get_user_info($wxid);
				$result['info'] = $info;
				die($this->jsonp->JSONP($_CODE['NEWS_GETNEWSINFO_SUCCESS'],$result));
			}
		}else
		{
			die($this->jsonp->JSONP($_CODE['NEWS_GETNEWSINFO_LOSE_NEWS_ID']));
		}
	}
	
	public function likeNew()
	{
		global $_CODE;
		if(isset($_REQUEST['wxid'])&&isset($_REQUEST['news_id']))
		{
			$wxid = intval($_REQUEST['wxid']);
			$news_id = intval($_REQUEST['news_id']);
			$result = $this->m->like_new($wxid,$news_id);
			if($result['id']){
				die($this->jsonp->JSONP($_CODE['NEWS_LIKENEW_SUCCESS'],$result));
			}else{
				die($this->jsonp->JSONP($_CODE['NEWS_LIKENEW_ERROR']));
			}
		}else
		{
			die($this->jsonp->JSONP($_CODE['NEWS_LIKENEW_LOSE_UID']));
		}
	}
	
	public function reply()
	{
		global $_CODE;
		if(isset($_REQUEST['wxid'])&&isset($_REQUEST['news_id']))
		{
			$info['comment'] = isset($_REQUEST['comment'])?$_REQUEST['comment']:'';
			if(empty($info['comment']))
			{
				die($this->jsonp->JSONP($_CODE['NEWS_REPLY_NO_COMMENT']));
			}
			
// 			$info['comment'] = json_encode($info['comment']); //暴露出unicode
// 			$info['comment'] = preg_replace("#(\\\ue[0-9a-f]{3})#ie","addslashes('\\1')",$info['comment']); //将emoji的
				    
			
			$info['commenter'] = intval($_REQUEST['wxid']);
			$info['news_id'] = intval($_REQUEST['news_id']);
			$info['parent_id'] = !empty($_REQUEST['parent_id'])?intval($_REQUEST['parent_id']):0;
			$info['comment_time'] = yf_time();
			$info['is_delete'] = 0;
			$result = $this->m->insert($info, "news_comment");
			if($result){
			    
			    $this->m->add_notify($info['news_id']);
			    
				$this->m->add_sub_number("news","comment_num", "+1", $info['news_id']);
				
				$_SESSION['notify'] = $_SESSION['notify'] +1;
				
				$res['id'] = $result;
				$res['comment_time'] = $info['comment_time'];
				die($this->jsonp->JSONP($_CODE['NEWS_REPLY_SUCCESS'],$res));
			}else{
				die($this->jsonp->JSONP($_CODE['NEWS_REPLY_ERROR']));
			}
		}else
		{
			die($this->jsonp->JSONP($_CODE['NEWS_REPLY_LOSE_UID']));
		}
		
	}
	
	public function delComment()
	{
		global $_CODE;
		if(isset($_REQUEST['wxid'])&&isset($_REQUEST['id']))
		{
			$wxid = intval($_REQUEST['wxid']);
			$comment_id = intval($_REQUEST['id']);
			$result = $this->m->del_comment($wxid,$comment_id);
			if($result){
				$this->m->add_sub_number("news","comment_num", "-1", intval($_REQUEST['news_id']));
				die($this->jsonp->JSONP($_CODE['NEWS_DELCOMMENT_SUCCESS'],$result));
			}else{
				die($this->jsonp->JSONP($_CODE['NEWS_DELCOMMENT_ERROR']));
			}
		}
		else
		{
			die($this->jsonp->JSONP($_CODE['NEWS_DELCOMMENT_LOSE_UID']));
		}
		
	}
	
	public function getCommunity()
	{
		global $_CODE;
		$wxid = $_SESSION['wxid']?$_SESSION['wxid']:0;
		$result = $this->m->get_community_info($wxid);
		$result['wxid'] = $wxid;
		die($this->jsonp->JSONP($_CODE['NEWS_GETCOMMUNITY_SUCCESS'],$result));
	}
	
	public function refreshCommunity()
	{
		global $_CODE;
		$wxid = intval($_REQUEST['wxid']);
		$startTime = isset($_GET['startTime'])?$_GET['startTime']:"";//下拉-头部
		$endTime = isset($_GET['endTime'])?$_GET['endTime']:"";//上拉-底部
		$result = $this->m->refresh_community_info($wxid,$startTime,$endTime);
		die($this->jsonp->JSONP($_CODE['NEWS_REFRESH_COMMUNITY_SUCCESS'],$result));
	}
	
	public function delNews()
	{
		global $_CODE;
		if(isset($_REQUEST['wxid'])&&isset($_REQUEST['id']))
		{
			$wxid = intval($_REQUEST['wxid']);
			$news_id = intval($_REQUEST['id']);
			$result = $this->m->del_news($wxid,$news_id);
			if($result){
				die($this->jsonp->JSONP($_CODE['NEWS_DELNEWS_SUCCESS'],$result));
			}else{
				die($this->jsonp->JSONP($_CODE['NEWS_DELNEWS_ERROR']));
			}
		}
		else
		{
			die($this->jsonp->JSONP($_CODE['NEWS_DELNEWS_LOSE_UID']));
		}
	
	}
	//改版首页
	public function getCommunityNew()
	{
		global $_CODE;
// 		$wxid = $_SESSION['wxid']?$_SESSION['wxid']:0;
		$catId = isset($_GET['catId'])?$_GET['catId']:"ALL";
		$search = isset($_GET['search'])?trim($_GET['search']):"";
		$result = $this->m->get_community_new_info($catId,$search);
// 		$result['wxid'] = $wxid;
		$result['notify'] = $_SESSION['notify'];
		die($this->jsonp->JSONP($_CODE['NEWS_GETCOMMUNITY_SUCCESS'],$result));
	}
	//改版下拉
	public function refreshCommunityNew()
	{
		global $_CODE;
		$catId = isset($_GET['catId'])?$_GET['catId']:"ALL";
		$search = isset($_GET['search'])?trim($_GET['search']):"";
		$page = isset($_GET['page'])?$_GET['page']:1;
		$result = $this->m->refresh_community_new_info($catId,$search,$page);
		die($this->jsonp->JSONP($_CODE['NEWS_REFRESH_COMMUNITY_SUCCESS'],$result));
		
	}
	//改版社区首页
	public function initTrendsNew(){
		global $_CODE;
		$wxid = $_SESSION['wxid']?$_SESSION['wxid']:0;
		$catId = isset($_GET['catId'])?$_GET['catId']:"ALL";
		$result = $this->m->get_trends_new_info($wxid,$catId);
		$result['wxid'] = $wxid;
		$result['notify'] = $_SESSION['notify'];
		die($this->jsonp->JSONP($_CODE['NEWS_INITTRENDS_SUCCESS'],$result));
	}
	
	public function refreshTrendsNew()
	{
		global $_CODE;
		$wxid = intval($_REQUEST['wxid']);
		$startTime = isset($_GET['startTime'])?$_GET['startTime']:"";//下拉-头部
		$endTime = isset($_GET['endTime'])?$_GET['endTime']:"";//上拉-底部
		
		$page = isset($_GET['page'])?$_GET['page']:"1";
		
		$reSet = isset($_GET['reSet'])?$_GET['reSet']:"0";//是否重置
		$tabId = isset($_GET['tabId'])?$_GET['tabId']:"newest";//最新/精品
		$catId = isset($_GET['catId'])?$_GET['catId']:"ALL";//类似
		$result = $this->m->refresh_trends_new_info($wxid,$startTime,$endTime,$reSet,$tabId,$catId,$page);
		//$result['reSet'] = $reSet;
		
		die($this->jsonp->JSONP($_CODE['NEWS_REFRESH_COMMUNITY_SUCCESS'],$result));
	}
}